home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Development / MosaicSRC / libwww2 / Unused / HTACL.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-13  |  5.6 KB  |  207 lines

  1.  
  2. /* MODULE                            HTACL.c
  3. **        ACCESS CONTROL LIST ROUTINES
  4. **
  5. ** AUTHORS:
  6. **    AL    Ari Luotonen    luotonen@dxcern.cern.ch
  7. **
  8. ** HISTORY:
  9. **
  10. **
  11. ** BUGS:
  12. **
  13. **
  14. */
  15.  
  16.  
  17. #include <stdio.h>    /* FILE */
  18. #include <string.h>
  19.  
  20. #include "HTUtils.h"
  21. #include "HTAAFile.h"    /* File routines    */
  22. #include "HTGroup.h"    /* GroupDef        */
  23. #include "HTACL.h"    /* Implemented here    */
  24.  
  25.  
  26. /* PUBLIC                        HTAA_getAclFilename()
  27. **        RESOLVE THE FULL PATHNAME OF ACL FILE FOR A GIVEN FILE
  28. ** ON ENTRY:
  29. **    path    is the pathname of the file for which to
  30. **        ACL file should be found.
  31. **
  32. **        ACL filename is computed by replacing
  33. **        the filename by .www_acl in the pathname
  34. **        (this is done to a local copy, of course).
  35. **
  36. ** ON EXIT:
  37. **    returns    the absolute pathname of ACL file
  38. **        (which is automatically freed next time
  39. **        this fuction is called).
  40. */
  41. PUBLIC char *HTAA_getAclFilename ARGS1(CONST char *, pathname)
  42. {
  43.     static char * local_copy = NULL;
  44.     static char * acl_path = NULL;
  45.     char * directory = NULL;
  46.     char * filename = NULL;
  47.  
  48.     StrAllocCopy(local_copy, pathname);    /* Also frees local_copy */
  49.                                         /* from previous call.   */
  50.  
  51.     directory = local_copy;
  52.     filename = strrchr(directory, '/');
  53.     if (!filename) {        /* No path in front of filename */
  54.     directory = ".";    /* So use current directory */
  55.     filename = local_copy;    /* and the pathname itself is the filename */
  56.     }
  57.     else {
  58.     *filename = '\0'; /* Truncate filename off from directory path */
  59.     filename++;      /* and the filename begins from the next character */
  60.     }
  61.     
  62.     StrAllocCopy(acl_path, directory);    /* Also frees acl_path */
  63.                                         /* from previous call. */
  64.     StrAllocCat(acl_path, "/");
  65.     StrAllocCat(acl_path, ACL_FILE_NAME);
  66.  
  67.     return acl_path;
  68. }
  69.  
  70.  
  71. /* PUBLIC                        HTAA_openAcl()
  72. **        OPEN THE ACL FILE FOR THE GIVEN DOCUMENT
  73. ** ON ENTRY:
  74. **    pathname    is the absolute pathname of
  75. **            the file to be accessed.
  76. **
  77. ** ON EXIT:
  78. **    returns        the FILE* to open ACL.
  79. **            NULL, if ACL not found.
  80. */
  81. PUBLIC FILE *HTAA_openAcl ARGS1(CONST char *, pathname)
  82. {
  83.     return fopen(HTAA_getAclFilename(pathname), "r");
  84. }
  85.  
  86.  
  87. /* PUBLIC                        HTAA_closeAcl()
  88. **            CLOSE ACL FILE
  89. ** ON ENTRY:
  90. **    acl_file is Access Control List file to close.
  91. **
  92. ** ON EXIT:
  93. **    returns    nothing.
  94. */
  95. PUBLIC void HTAA_closeAcl ARGS1(FILE *, acl_file)
  96. {
  97.     if (acl_file)  fclose(acl_file);
  98. }
  99.  
  100.  
  101. /* PUBLIC                        HTAA_getAclEntry()
  102. **            CONSULT THE ACCESS CONTROL LIST AND
  103. **            GIVE A LIST OF GROUPS (AND USERS)
  104. **            AUTHORIZED TO ACCESS A GIVEN FILE
  105. ** ON ENTRY:
  106. **    acl_file    is an open ACL file.
  107. **    pathname    is the absolute pathname of
  108. **            the file to be accessed.
  109. **    method        is the method for which access is wanted.
  110. **
  111. ** ALC FILE FORMAT:
  112. **
  113. **    template : method, method, ... : group@addr, user, group, ...
  114. **
  115. **    The last item is in fact in exactly the same format as
  116. **    group definition in group file, i.e. everything that
  117. **    follows the 'groupname:' part,
  118. **    e.g.
  119. **        user, group, user@address, group@address,
  120. **        (user,group,...)@(address, address, ...)
  121. **
  122. ** ON EXIT:
  123. **    returns        NULL, if there is no entry for the file in the ACL,
  124. **            or ACL doesn't exist.
  125. **            If there is, a GroupDef object containing the
  126. **            group and user names allowed to access the file
  127. **            is returned (this is automatically freed
  128. **            next time this function is called).
  129. ** IMPORTANT:
  130. **    Returns the first entry with matching template and
  131. **    method. This function should be called multiple times
  132. **    to process all the valid entries (until it returns NULL).
  133. **    This is because there can be multiple entries like:
  134. **
  135. **        *.html : get,put : ari,timbl,robert
  136. **        *.html : get     : jim,james,jonathan,jojo
  137. **
  138. ** NOTE:
  139. **    The returned group definition may well contain references
  140. **    to groups defined in group file. Therefore these references
  141. **    must be resolved according to that rule file by function
  142. **    HTAA_resolveGroupReferences() (group file is read in by
  143. **    HTAA_readGroupFile()) and after that access authorization
  144. **    can be checked with function HTAA_userAndInetGroup().
  145. */
  146. PUBLIC GroupDef *HTAA_getAclEntry ARGS3(FILE *,        acl_file,
  147.                     CONST char *,    pathname,
  148.                     HTAAMethod,    method)
  149. {
  150.     static GroupDef * group_def = NULL;
  151.     CONST char * filename;
  152.     int len;
  153.     char *buf;
  154.  
  155.     if (!acl_file) return NULL;        /* ACL doesn't exist */
  156.     
  157.     if (group_def) {
  158.     GroupDef_delete(group_def);    /* From previous call */
  159.     group_def = NULL;
  160.     }
  161.  
  162.     if (!(filename = strrchr(pathname, '/')))
  163.     filename = pathname;
  164.     else filename++;    /* Skip slash */
  165.  
  166.     len = strlen(filename);
  167.  
  168.     if (!(buf = (char*)malloc((strlen(filename)+2)*sizeof(char))))
  169.     outofmem(__FILE__, "HTAA_getAuthorizedGroups");
  170.     
  171.     while (EOF != HTAAFile_readField(acl_file, buf, len+1)) {
  172.     if (HTAA_templateMatch(buf, filename)) {
  173.         HTList *methods = HTList_new();
  174.         HTAAFile_readList(acl_file, methods, MAX_METHODNAME_LEN);
  175.         if (TRACE) {
  176.         fprintf(stderr,
  177.             "Filename '%s' matched template '%s', allowed methods:",
  178.             filename, buf);
  179.         }    
  180.         if (HTAAMethod_inList(method, methods)) {    /* right method? */
  181.         if (TRACE) fprintf(stderr, " METHOD OK\n");
  182.         HTList_delete(methods);
  183.         free(buf);
  184.         group_def = HTAA_parseGroupDef(acl_file);
  185.         return group_def;
  186.         }
  187.         else if (TRACE) fprintf(stderr, " METHOD NOT FOUND\n");
  188.         HTList_delete(methods);
  189.     }    /* if template match */
  190.     else {
  191.         HTAAFile_nextRec(acl_file);
  192.         if (TRACE) {
  193.         fprintf(stderr,
  194.             "Filename '%s' didn't match template '%s'\n",
  195.             filename, buf);
  196.         }
  197.     }
  198.  
  199.     HTAAFile_nextRec(acl_file);
  200.     }    /* while not eof */
  201.     free(buf);
  202.  
  203.     return NULL;    /* No entry for requested file */
  204.                         /* (or an empty entry).        */
  205. }
  206.  
  207.